package edu.njust.controller;

import edu.njust.Service.Room.RoomService;
import edu.njust.Service.bill.BillService;
import edu.njust.Service.commodity.CommodityService;
import edu.njust.Service.stayregister.StayRegisterService;
import edu.njust.controller.depcontroller.Code;
import edu.njust.controller.depcontroller.Result;
import edu.njust.domain.Bill;
import edu.njust.domain.Commodity;
import edu.njust.domain.Consumer;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/CommodityController")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private StayRegisterService stayRegisterService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BillService billService;

    //通过测试
    @DeleteMapping("/{id}")
    public Result deleteCommodity(@PathVariable int id){
        boolean flag = commodityService.deleteCommodity(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    @PostMapping
    //插入商品
    public Result insertCommodity(@RequestBody Commodity commodity){
        boolean flag = commodityService.insertCommodity(commodity);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable int id) {
        Commodity commodity = commodityService.selectById(id);
        Integer code = commodity != null ? Code.GET_OK : Code.GET_ERR;
        String msg = commodity != null ? "" : "数据查询失败，请重试！";
        return new Result(code,commodity,msg);
    }

    @PutMapping
    public Result updateById(@RequestBody Commodity commodity) {
        boolean flag = commodityService.updateById(commodity);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
    @GetMapping("/pageSelect/{commodityName}/{commodityTypeID}/{start}/{pageSize}")
    public Result pageSelect(@PathVariable String commodityName,
                             @PathVariable int commodityTypeID,
                             @PathVariable int start,
                             @PathVariable int pageSize) {
        List<Commodity> commodities=commodityService.pageSelect(commodityName,commodityTypeID,start,pageSize);
        Integer code = commodities != null ? Code.GET_OK : Code.GET_ERR;
        String msg = commodities != null ? "" : "数据查询失败，请重试！";
        return new Result(code,commodities,msg);
    }

    @GetMapping("/countSelect/{commodityName}/{commodityTypeID}")
    public Result countSelect(@PathVariable String commodityName, @PathVariable int commodityTypeID) {
        Integer num = commodityService.countSelect(commodityName,commodityTypeID);
        Integer code = num != null ? Code.GET_OK : Code.GET_ERR;
        String msg = num != null ? "" : "数据查询失败，请重试！";
        return new Result(code,num,msg);
    }

    @GetMapping("/selectYZ/{commodityName}")
    public Result selectYZ(@PathVariable String commodityName) {
        boolean flag = commodityService.selectYZ(commodityName);
        return new Result(Code.GET_OK,flag);
    }


    @GetMapping("/transRoomNumToStayRister/{roomNum}")
    public Result transRoomNumToStayRister(@PathVariable String roomNum){
        int stayristerid = stayRegisterService.selectByRoom(roomService.selectidbyname(roomNum));
        return new Result(Code.GET_OK,stayristerid);
    }

    @PostMapping("/insertBill")
    public Result insertBill(@RequestBody Bill bill){
        float price= Float.parseFloat(commodityService.selectById(bill.getCommodityID()).getSalePrice());
        //计算消费价格并设置
        bill.setConsumptionMoney(price*Float.parseFloat(bill.getConsumptionNumber()));
        boolean flag = billService.insertBill(bill);

        //System.out.println(bill);

        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

}
